package cn.com.css.taxInspection.attention.service.impl;

import cn.com.css.taxInspection.attention.entity.AttentionCase;
import cn.com.css.taxInspection.attention.entity.XazbDl;
import cn.com.css.taxInspection.attention.entity.XazbMx;
import cn.com.css.taxInspection.attention.entity.XazbNsr;
import cn.com.css.taxInspection.attention.mapper.AttentionMapper;
import cn.com.css.taxInspection.attention.mapper.XazbDlMapper;
import cn.com.css.taxInspection.attention.mapper.XazbMxMapper;
import cn.com.css.taxInspection.attention.service.AttentionService;
import cn.com.css.taxInspection.common.exception.BusinessException;
import cn.com.css.taxInspection.common.exception.code.BaseResponseCode;
import cn.com.css.taxInspection.system.service.impl.HttpSessionService;
import cn.com.css.taxInspection.system.vo.res.PermissionRespNode;
import cn.com.css.taxInspection.thirdparty.entity.CqyjDetailData;
import cn.com.css.taxInspection.thirdparty.mapper.CqyjDetailDataMapper;
import cn.com.css.taxInspection.thirdparty.vo.InspectRequestData;
import cn.com.css.taxInspection.util.TsBqUtils;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

@Service
@Slf4j
public class AttentionServiceImpl extends ServiceImpl<AttentionMapper, AttentionCase> implements AttentionService {

    @Resource
    private AttentionMapper attentionMapper;
    @Resource
    private CqyjDetailDataMapper cqyjDetailDataMapper;
    @Resource
    private HttpSessionService httpSessionService;
    @Resource
    private XazbDlMapper xazbDlMapper;
    @Resource
    private XazbMxMapper xazbMxMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    @DS("oracle")
    public void addCase(AttentionCase vo) {
//        vo.setStatus(Constants.STATUS_NORMAL);
        vo.setCreateTime(new Date());
        vo.setUpdateTime(null);
        attentionMapper.insert(vo);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    @DS("oracle")
    public void updateCase(AttentionCase vo) {
        AttentionCase attentionCase = attentionMapper.selectById(vo.getId());
        if (null == attentionCase) {
            log.error("传入 的 id:{}不合法", vo.getId());
            throw new BusinessException(BaseResponseCode.DATA_ERROR);
        }
        vo.setUpdateTime(new Date());
        attentionMapper.updateById(vo);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    @DS("oracle")
    public void deletedCase(String id) {
        attentionMapper.deleteById(id);
    }

    @Override
    @DS("oracle")
    public AttentionCase detailInfo(String id) {
        AttentionCase attentionCase = attentionMapper.selectById(id);
        if (attentionCase == null) {
            log.error("传入 的 id:{}不合法", id);
            throw new BusinessException(BaseResponseCode.DATA_ERROR);
        }
        return attentionCase;
    }

    @Override
    @DS("oracle")
    public List<PermissionRespNode> selectAllCaseTree() {
        String currentSwjgDm = httpSessionService.getCurrentDeptNo();
        String swjgDmTmp = currentSwjgDm.substring(0, 9);
        List<AttentionCase> result = attentionMapper
                .selectList(Wrappers.<AttentionCase>lambdaQuery().eq(AttentionCase::getStatus, 1)
                        .eq(AttentionCase::getSwjgDm, swjgDmTmp + "00")
                        .orderByAsc(AttentionCase::getCreateTime));
        List<PermissionRespNode> childs = new ArrayList<>();
        PermissionRespNode child;
        for (AttentionCase attentionCase : result) {
            child = new PermissionRespNode();
            child.setId(attentionCase.getId());
            child.setTitle(attentionCase.getName());
            childs.add(child);
        }

        List<PermissionRespNode> res = new ArrayList<>();
        PermissionRespNode firstLevel = new PermissionRespNode();
        firstLevel.setId("0");
        firstLevel.setTitle("专项案件");
        firstLevel.setSpread(true);
        firstLevel.setChildren(childs);
        res.add(firstLevel);
        return res;
    }

    @Override
    public List<PermissionRespNode> selectAllXazbTree() {
        String currentSwjgDm = httpSessionService.getCurrentDeptNo();
        String swjgDmTmp = currentSwjgDm.substring(0, 9);
        List<XazbDl> result = xazbDlMapper
                .selectList(Wrappers.<XazbDl>lambdaQuery().eq(XazbDl::getStatus, 1)
                        .eq(XazbDl::getSwjgDm, swjgDmTmp + "00")
                        .orderByAsc(XazbDl::getId));
        List<PermissionRespNode> childs = new ArrayList<>();
        PermissionRespNode child;
        for (XazbDl xazbDl : result) {
            child = new PermissionRespNode();
            child.setId(xazbDl.getId());
            child.setTitle(xazbDl.getName());
            childs.add(child);
        }
        return childs;
    }

    @Override
    public List<XazbMx> getXazbMxByXadlId(String xazbDlId) {
        return xazbMxMapper
                .selectList(Wrappers.<XazbMx>lambdaQuery().eq(XazbMx::getStatus, 1).eq(XazbMx::getXazbdlid, xazbDlId));
    }

    @Override
    @DS("oracle")
    public Map<String, Object> getNsrListByZbmxId(XazbNsr req) {
        Map<String, Object> resData = new HashMap<>();
        List<XazbNsr> resNsrList;
        List<String> zbmxIdsList = Arrays.asList(req.getZbmxIds());
        int idsLength = zbmxIdsList.size();
        if (idsLength > 1) {
            resNsrList = attentionMapper.getNsrListByZbmxIdMany(zbmxIdsList, idsLength);
        } else {
            resNsrList = attentionMapper.getNsrListByZbmxIdOne(zbmxIdsList);
        }
        resData.put("records", resNsrList);
        resData.put("total", resNsrList.size());
        return resData;
    }

    @Override
    public List<XazbDl> zblxList() {
        String currentSwjgDm = httpSessionService.getCurrentDeptNo();
        String swjgDmTmp = currentSwjgDm.substring(0, 9);
        return xazbDlMapper
                .selectList(Wrappers.<XazbDl>lambdaQuery().eq(XazbDl::getStatus, 1)
                        .eq(XazbDl::getSwjgDm, swjgDmTmp + "00")
                        .orderByAsc(XazbDl::getId));
    }


    @Override
    @DS("oracle")
    public Map<String, Object> getAjxxByJcajuuid(String jcajxxuuid) {
        return attentionMapper.getAjxxMapByJcajuuid(jcajxxuuid);
    }

    @Override
    @DS("oracle")
    public List<Map<String, Object>> ajyjTableFirstLevel(String swjgDm) {
        String currentSwjgDm = httpSessionService.getCurrentDeptNo();
        String currentRoleIds = httpSessionService.getCurrentRolesId();
        String dataScope = TsBqUtils.getDataScopeByRolesId(currentRoleIds);
        List<Map<String, Object>> resMap = null;
        if (TsBqUtils.isNotNull(swjgDm)) {
            resMap = attentionMapper.ajyjTableFirstLevelKs(swjgDm.substring(0, 9));
        } else {
            if ("jcj".equals(dataScope) || "jck".equals(dataScope)) {  //dataScope=数据权限：jcj(稽查一局)、all(沈阳市局)、jck(检查科)
                resMap = attentionMapper.ajyjTableFirstLevelKs(currentSwjgDm.substring(0, 9));
            } else if ("all".equals(dataScope)) {
                resMap = attentionMapper.ajyjTableFirstLevel();
            }
        }
        return resMap;
    }

    @Override
    @DS("oracle")
    public Page getCqyjDetailDataForXzs(InspectRequestData req) {
        Page page = new Page(req.getPage(), req.getLimit());
        LambdaQueryWrapper<CqyjDetailData> queryWrapper = Wrappers.lambdaQuery();
        String swjgDm = req.getParam().getCqyjDetailParams().getSwjgDm();
        String dayType = req.getParam().getCqyjDetailParams().getDayType();
        String datasource = req.getParam().getCqyjDetailParams().getDatasource();//chart 是右侧图表点击  table是表格数据点击
        queryWrapper.isNotNull(CqyjDetailData::getBz);
        if ("chart".equals(datasource)) {
            queryWrapper.eq(CqyjDetailData::getSwjgDm, swjgDm);
        } else {
            String level = req.getParam().getCqyjDetailParams().getLevel();
            if ("sj".equals(level)) {
                queryWrapper.eq(CqyjDetailData::getSwjgDm, swjgDm);
            } else {
                queryWrapper.eq(CqyjDetailData::getJcbmDm, swjgDm);
            }
        }
        List<String> bhsByDayType = getBhsByDayType(dayType);
        queryWrapper.in(CqyjDetailData::getBh, bhsByDayType);
        Page resPage = cqyjDetailDataMapper.selectPage(page, queryWrapper);
        List<CqyjDetailData> cqyjDetailDatas = resPage.getRecords();
        for (CqyjDetailData o : cqyjDetailDatas) {
            String larq = o.getLarq();
            o.setLarq(larq.substring(0, 10));
            o.setJcbmmc(o.getJcbmmc().replace("国家税务总局沈阳市税务局第一稽查局", ""));
        }
        resPage.setRecords(cqyjDetailDatas);
        return resPage;
    }

    @Override
    @DS("oracle")
    public Map<String, List> chartData() {
        Map<String, List> resMap = new HashMap<>();
        List<Object> XY30SL = new ArrayList<>();
        List<Object> DY30TO60SL = new ArrayList<>();
        List<Object> DY60TO90SL = new ArrayList<>();
        List<Object> DY90SL = new ArrayList<>();
        List<Map<String, Object>> chartMapData = attentionMapper.ajyjTableFirstLevel();
        for (Map objMap : chartMapData) {
            XY30SL.add(Integer.valueOf(String.valueOf(objMap.get("xy30sl"))));
            DY30TO60SL.add(Integer.valueOf(String.valueOf(objMap.get("dy30to60sl"))));
            DY60TO90SL.add(Integer.valueOf(String.valueOf(objMap.get("dy60to90sl"))));
            DY90SL.add(Integer.valueOf(String.valueOf(objMap.get("dy90sl"))));
        }
        resMap.put("xy30sl", XY30SL);
        resMap.put("dy30to60sl", DY30TO60SL);
        resMap.put("dy60to90sl", DY60TO90SL);
        resMap.put("dy90sl", DY90SL);
        return resMap;
    }

    private List<String> getBhsByDayType(String dayType) {
        List<String> bhs = null;
        switch (dayType) {
            case "XY30SL":
                bhs = Arrays.asList("8");
                break;
            case "DY30TO60SL":
                bhs = Arrays.asList("1", "2", "3");
                break;
            case "DY60TO90SL":
                bhs = Arrays.asList("4", "5", "6");
                break;
            case "DY90SL":
                bhs = Arrays.asList("7");
                break;
            case "FIRST_XY30SL":
                bhs = Arrays.asList("8");
                break;
            case "FIRST_DY30TO60SL":
                bhs = Arrays.asList("1", "2", "3");
                break;
            case "FIRST_DY60TO90SL":
                bhs = Arrays.asList("4", "5", "6");
                break;
            case "FIRST_DY90SL":
                bhs = Arrays.asList("7");
                break;
        }
        return bhs;
    }
}
